package com.sky.happygos.present;

import android.util.Log;

import com.google.gson.Gson;
import com.sky.happygos.base.ErrorHandler;
import com.sky.happygos.base.Event;
import com.sky.happygos.event.GoodsE;
import com.sky.happygos.event.LoginE;

import com.sky.happygos.event.RegisterE;
import com.sky.happygos.event.ShopE;
import com.sky.happygos.event.UpdateUserE;
import com.sky.happygos.event.UserMeE;
import com.sky.happygos.model.Goods;
import com.sky.happygos.model.Login;
import com.sky.happygos.model.RestMessage;
import com.sky.happygos.model.Shop;
import com.sky.happygos.model.Token;
import com.sky.happygos.model.User;
import com.sky.happygos.network.Api;
import com.sky.happygos.network.ApiService;
import com.sky.happygos.network.TokenPrefs;
import com.sky.happygos.network.TokenPrefs_;
import com.squareup.okhttp.ResponseBody;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static org.androidannotations.annotations.EBean.Scope.Singleton;

/**
 * Created by qqqqqqq on 2017/4/16.
 */
@EBean(scope = Singleton)
public class UserP {

    @Pref
    TokenPrefs_ tokenPrefs;

    @Bean
    Api api;

    @Background
    public  void login(long phone,String password) {
        Call<Token> call = api.getService().login(new Login(phone,password));
        call.enqueue(new Callback<Token>() {
            @Override
            public void onResponse(Call<Token> call, Response<Token> response) {
                if(response.code() == 200) {
                    Token token = response.body();
                    tokenPrefs.edit().token().put(token.token).apply();
                    tokenPrefs.edit().userid().put(token.userid).apply();
                    Log.v("token:===>",tokenPrefs.token().get());
                    Log.v("userid:===>",tokenPrefs.userid().get());

                    EventBus.getDefault().post(new LoginE(token,true));
                }else{
                    EventBus.getDefault().post(new LoginE(response));
                }
            }

            @Override
            public void onFailure(Call<Token> call, Throwable t) {
                EventBus.getDefault().post(new LoginE(null));
            }
        });
    }

    @Background
    public void register(long phone,String password){
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        builder.addFormDataPart("phone",phone+"");
        builder.addFormDataPart("password",password);
        builder.addFormDataPart("type",1+"");
//        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//        builder.addFormDataPart("imgfile", file.getName(), imageBody);
        List<MultipartBody.Part> parts = builder.build().parts();

        Call<RestMessage> call = api.getService().register(parts);
        call.enqueue(new Callback<RestMessage>() {
            @Override
            public void onResponse(Call<RestMessage> call, Response<RestMessage> response) {

                if(response.code() == 200) {
                    RestMessage g = response.body();
                    EventBus.getDefault().post(new RegisterE(g,true));
                }else{
                    EventBus.getDefault().post(new RegisterE(response));
                }
            }

            @Override
            public void onFailure(Call<RestMessage> call, Throwable t) {
                EventBus.getDefault().post(new RegisterE(null));
            }
        });
    }

    @Background
    public void getUser(){

        Call<User> call = api.getAuthService().getMe();
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {

                if(response.code() == 200) {
                    User g = response.body();
                    EventBus.getDefault().post(new UserMeE(g,true));
                }else{
                    EventBus.getDefault().post(new UserMeE(response));
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                EventBus.getDefault().post(new UserMeE(null));
            }
        });
    }

    @Background
    public void updateUser(String path,User user){
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        builder.addFormDataPart("info",user.info);
        builder.addFormDataPart("name",user.name);

        if(path != null) {
            File file = new File(path);
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("icon", file.getName(), imageBody);
        }

        List<MultipartBody.Part> parts = builder.build().parts();

        Call<User> call = api.getAuthService().updateUser(parts);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {

                if(response.code() == 200) {
                    User g = response.body();
                    EventBus.getDefault().post(new UpdateUserE(g,true));
                }else{
                    EventBus.getDefault().post(new UpdateUserE(response));
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                EventBus.getDefault().post(new UpdateUserE(null));
            }
        });
    }





}
